﻿Imports System.Data.OleDb
Public Class ViewAlbum
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Params("ID") = "" Then
            Response.Redirect(String.Format("Profile.aspx?Tourist={0}", User.Identity.Name))
        End If

        If IsPostBack = False Then
            'Count activity
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
            Dim SqlString As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Albums")
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            'Assign text to heading
            Dim SqlStringAlbumDetails As String = "SELECT * FROM Albums WHERE ID = @f1"
            Dim cmdAlbumDetails As OleDbCommand = New OleDbCommand(SqlStringAlbumDetails, oleDbConn)
            cmdAlbumDetails.CommandType = CommandType.Text
            cmdAlbumDetails.Parameters.AddWithValue("@f1", Request.Params("ID"))

            Dim drAlbumDetails As OleDbDataReader = cmdAlbumDetails.ExecuteReader()
            drAlbumDetails.Read()
            If drAlbumDetails.HasRows Then
                myHeading.InnerHtml = drAlbumDetails("AlbumName") & " by " & drAlbumDetails("Tourist")
            End If

            'Check if user has voted this album
            Dim hasVotedsql As String = "SELECT * FROM Album_Votes WHERE Album_FK = @f1 AND Voter = @f2"
            Dim hasVotedcmd As OleDbCommand = New OleDbCommand(hasVotedsql, oleDbConn)
            hasVotedcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            hasVotedcmd.Parameters.AddWithValue("@f2", User.Identity.Name)

            'Find out if there any records at all (returned)
            Dim drVotes As OleDbDataReader = hasVotedcmd.ExecuteReader()
            'this shows if any data is returned
            If drVotes.HasRows Then
                btn_VoteAlbum.Enabled = False
                btn_VoteAlbum.Text = "Voted"
            Else
                btn_VoteAlbum.Enabled = True
                btn_VoteAlbum.Text = "Vote"
            End If
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub btn_VoteAlbum_Click(sender As Object, e As EventArgs) Handles btn_VoteAlbum.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim addVotesql As String = "INSERT INTO Album_Votes (Album_FK, Voter) VALUES (@f1, @f2)"
        Dim addVotecmd As OleDbCommand = New OleDbCommand(addVotesql, oleDbConn)
        addVotecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        addVotecmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addVotecmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        addVotecmd.Dispose()
        'Update the repeater to display the vote
        rpt_Votes.DataBind()
        'hide button
        btn_VoteAlbum.Enabled = False
        btn_VoteAlbum.Text = "Voted"
        
    End Sub
End Class